package com.itheima.controller;


import cn.hutool.core.io.FileUtil;
import com.itheima.config.AliyunOSSProperties;
import com.itheima.pojo.Result;
import com.itheima.util.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
/*    @Value("${aliyun.oss.endpoint}")
    private String endpoint ;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName ;*/
    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();//a.jpg   b.jpg

        //获取随机 的文件名
        String uuid = UUID.randomUUID().toString();

        //获取文件后缀名
        String suffix = FileUtil.getSuffix(filename);

        filename=uuid+"."+suffix;

        //调用阿里云工具类方法
        String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getBucketName(), file.getBytes(), filename);

        return Result.success(url);
    }
}
